<?php
//------- logistic wxapp -------------------------------------------------------------
namespace api\user\controller;

use cmf\controller\RestUserBaseController;
use think\Db;
use think\Validate;

class AddressController extends RestUserBaseController {

    // 更新地址
    public function edit() {
        $data = $this->request->param();
        $userId = $this->getUserId();
        if (isset($data['id']) and !empty($data['id'])) {  //更新
        } else {  //新加
            $db = Db::name("address");
            $addr_where = ['user_id'=>$userId,'type'=>3,'status'=>1];
            $count = $db->where($addr_where)->count();
            if (!empty($count)) {  //已有，需保证只能9个
                if (intval($count)>8) {
                    $this->error("最多仅可绑定9个地址!");
                } else {
                    for ($i=1; $i < 10; $i++) { 
                        $addr_where['code'] = $i;
                        $find_me = $db->where($addr_where)->count();
                        if (empty($find_me)) {
                            $code = $i;break;
                        }
                    }
                }
            } else {   //还没数据，自增1
                $code = 1;
            }
            if (!empty($data['stage'])) {
                $stage = $db->where(['id'=>$data['stage']])->value('code');
            } else {
                $this->error("请选择驿站!");
            }
            if (!empty($data['station'])) {
                $station = $db->where(['id'=>$data['station']])->value('code');
            } else {
                $this->error("请选择小站!");
            }
            if (empty($data['country'])) {
                $this->error("请输入国家!");
            } else {
                $coun_id = cmf_get_region_id($data['country'],1);
            }
            if (empty($data['province'])) {
                $this->error("请输入省/州!");
            } else {
                $pro_id = cmf_get_region_id($data['province'],2,$coun_id);
            }
            if (empty($data['city'])) {
                $this->error("请输入市/区!");
            } else {
                $city_id = cmf_get_region_id($data['city'],3,$pro_id);
            }
            if (empty($data['detail'])) {
                $this->error("请输入地址!");
            }
            if (empty($data['receive'])) {
                $this->error("请输入收件人!");
            }
            if (empty($data['zone'])) {
                $this->error("请输入国际区号!");
            }
            if (empty($data['contact'])) {
                $this->error("请输入联系方式!");
            }
            if (empty($data['zip'])) {
                $this->error("请输入邮编!");
            }
            $fin_code = $userId.$stage.$station.$code;
            
            if (is_usa($data['country'])) {
                $name = $data['country'].' '.$data['detail'].', '.$data['city'].', '.$data['province'].' '.$data['zip'];
            } else {
                $name = $data['country'].' '.$data['province'].' '.$data['city'].' '.$data['detail'].'，'.$data['zip'];
            }

            $add = [
                'user_id'=>$userId,'code'=>$code,'fin_code'=>$fin_code,'zone'=>$data['zone'],'contact'=>$data['contact'],'receive'=>$data['receive'],'zip'=>$data['zip'],'type'=>3,'status'=>1,
                'station'=>$data['station'],'stage'=>$data['stage'],'detail'=>$data['detail'],
                'name'=>$name,'coun_id'=>$coun_id,'pro_id'=>$pro_id,'city_id'=>$city_id
            ];
            $id = $db->insertGetId($add);
            
        }
        

        $this->success("操作成功!");
    }

    public function getList() {
        $data = [];
        $userId   = $this->getUserId();
        $fieldStr = 'a.*,s.name as s_name';
        $where = ['a.user_id'=>$userId,'a.type'=>3,'a.status'=>1];
        $join = [
            ['__ADDRESS__ s', 'a.stage = s.id and s.type = 1']
        ];
        $data = Db::name("address")->alias('a')->join($join)->where($where)->order('a.code asc')->field($fieldStr)->select();
        $this->success('获取成功！', $data);
    }

    public function read() {
        $param = $this->request->param();
        $userId = $this->getUserId();
        $fieldStr = '*';
        $where = ['id'=>$param['id'],'user_id'=>$userId,'type'=>3];
        $data = Db::name("address")->where($where)->field($fieldStr)->find();
        $data['zone'] = has_plus($data['zone']);
        $data['country'] = Db::name("regions")->where(['id'=>$data['coun_id']])->value('name');
        if (!empty($data['pro_id'])) {
            $data['province'] = Db::name("regions")->where(['id'=>$data['pro_id']])->value('name');
        } else {
            $data['province'] = '';
        }
        
        if (!empty($data['city_id'])) {
            $data['city'] = Db::name("regions")->where(['id'=>$data['city_id']])->value('name');
        } else {
            $data['city'] = '';
        }
        
        $station = Db::name("address")->where(['id'=>$data['station']])->field('name,contact,receive,zone')->find();
        $data['station'] = $station['name'];
        $data['station_receive'] = $station['receive'];
        $data['station_zone'] = has_plus($station['zone']);
        $data['station_contact'] = $station['contact'];
        $stage = Db::name("address")->where(['id'=>$data['stage']])->field('name,contact,receive,zone')->find();
        $data['stage'] = $stage['name'];
        $data['stage_zone'] = has_plus($stage['zone']);
        $data['stage_contact'] = $stage['contact'];
        $data['stage_receive'] = $stage['receive'];
        $this->success('获取成功！', $data);
    }

    public function delete() {
        $param = $this->request->param();
        $userId = $this->getUserId();
        $where = ['id'=>$param['id'],'user_id'=>$userId,'type'=>3];
        $data = Db::name("address")->where($where)->update(['status'=>0]);
        $this->success('删除成功！');
    }

    // 更新用户form_id
    public function update() {
        $param = $this->request->param();
        $param['user_id'] = $this->getUserId();
        cmf_set_form_id($param);
        $this->success('成功！');
    }

}
